	
********************************************************************************
*** Time shifts for synthetic controls

*** First remove FEs
cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/robustness/feout
use /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/types_panel.dta, clear

reg pmt_amt i.dt 
predict pmt_nofe, residuals
drop pmt_amt
rename pmt_nofe pmt_amt

save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/robustness/feout/types_panel_feout.dta


********************************************************************************
*** Time shifts for synthetic controls

* load panel without FE

cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/robustness/feout
use /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/robustness/feout/types_panel_feout.dta, clear 
*********************************************************************
*** Regress Outliers on lags to find best fit  
levelsof group, local(levels)
keep group dt pmt_amt 
reshape wide pmt_amt, i(dt) j(group)
tsset dt
matrix distanceLag = (0,0,0)
matrix distanceLead = (0,0,0)
* 3 columns: group, lag period, distance

set matsize 11000

** Only want to keep outliers pre-period 
local treatment_period = mofd(mdy(11, 4, 2002))
replace pmt_amt999 = . if dt >= `treatment_period'

** Find max lead so that there are 5 years of post-period data 
summarize dt 
local maxlead = r(max) - `treatment_period' + 1 - 60  
	** Max - treatment  + 1 = length of end window 
	** Take 60 off and that's the max you can shift before running out of data

** Find max lag so that there are 3 years of pre-period data 
summarize dt 
local maxlag = `treatment_period' - r(min) - 36
	** Treatment period - min  = length of end window 
	** Take 60 off and that's the max you can shift before running out of data


gen diff = 0 
*** Loop, storing leads lags and fit
foreach g of local levels {
	disp `g'
	* Skip Outlier treated group
	if `g' == 999{
			continue
		}

	foreach t of numlist 0/`maxlag'{
		*disp `t'
		qui replace diff =  (pmt_amt999 - L`t'.pmt_amt`g')^2
		qui summarize diff 
		matrix distanceLag = (distanceLag \ `g', `t', r(sum)/r(N))
		}

	foreach t of numlist 0/`maxlead'{
		* disp `t'
		qui replace diff = (pmt_amt999 - F`t'.pmt_amt`g')^2
		qui summarize diff 
		matrix distanceLead = (distanceLead \ `g', `t', r(sum)/r(N))
		}
}

svmat distanceLag
keep distanceLag*
svmat distanceLead

rename distanceLag1 laggroup
rename distanceLag2 lagperiod
rename distanceLag3 lagdistance
rename distanceLead1 leadgroup
rename distanceLead2 leadperiod
rename distanceLead3 leaddistance

drop if laggroup == 0
save distancelaglead.dta, replace

********************************************************************************
*** Find best fit per group

cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/robustness/feout
use distancelaglead.dta, clear

preserve
keep laggroup lagperiod lagdistance
drop if mi(laggroup)
bysort laggroup: egen minlagdistance = min(lagdistance)
keep if lagdistance == minlagdistance
drop minlagdistance
rename laggroup group
save group_lag_mindistance.dta, replace

restore
keep leadgroup leadperiod leaddistance
drop if mi(leadgroup)
bysort leadgroup: egen minleaddistance = min(leaddistance)

keep if leaddistance == minleaddistance
drop minleaddistance
rename leadgroup group
save group_lead_mindistance.dta, replace

clear
use group_lag_mindistance.dta
merge 1:1 group using group_lead_mindistance.dta

drop _merge
bysort group: gen mindistance = min(leaddistance, lagdistance)

replace lagdistance = . if lagdistance!= mindistance
replace leaddistance = . if leaddistance!= mindistance

replace lagperiod = . if lagdistance == .
replace leadperiod = . if leaddistance == .

replace lagperiod = -1*lagperiod
gen period = min(lagperiod, leadperiod)
	* Min because missing is infty 

keep group period mindistance
drop if mi(group)
save groupleadlags.dta, replace

********************************************************************************





